package com.bnz.cart.controller;

import com.bnz.cart.entity.group.Cart;
import com.bnz.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class CatController {
	@Autowired
	private CartService cartService;
	//1. 默认欢迎页面
	@GetMapping({"/","/cart.html"})
	public String index(Model model,HttpSession session){
		//1.1 得到登录名
		String name = session.getAttribute("name") + "";
		//1.3 根据登录名取得购物车
		List<Cart> cartList = cartService.getCartList(name);
		//1.2 将购物车列表放到model中
		model.addAttribute("cartList",cartList);
		return "cart";
	}


	/**
	 * 2. 添加购物车
	 * @param itemId sku商品id
	 * @param num    购买的商品数量
	 * @return
	 */
	@GetMapping("/addCart/{itemId}/{num}")
	public String addCart(@PathVariable Long itemId, @PathVariable int num, HttpSession session,Model model){
		//2.1 得到当前的登录用户名
		String name = session.getAttribute("name") + "";
		//2.2 添加购物车到redis中
		System.out.println("name = " + name);
		System.out.println("itemId = " + itemId);
		System.out.println("num = " + num);
		List<Cart> cartList = cartService.addCart(itemId,num,name);
		//2.3 放到model中
		model.addAttribute("cartList",cartList);
		return "cart";
	}
}
